/**
 * 
 */
package com.dhcc.frame.guava.str;

import static org.junit.Assert.assertEquals;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

import org.junit.Test;

import com.google.common.base.CharMatcher;
import com.google.common.base.Predicate;
/**
 * @author 谢进军
 *
 */
public class CharMatcherTest {
	@Test public void testCharMatcher(){
		String input = "H*el.lo,}12";
		CharMatcher matcher = CharMatcher.JAVA_LETTER_OR_DIGIT;
		String result = matcher.retainFrom(input);
		System.out.println(result);
	}
	@Test public void whenRemoveNonASCIIChars_thenRemoved() {
	    String input = "あhello₤";
	 
	    String result = CharMatcher.ASCII.retainFrom(input);
	    assertEquals("hello", result);
	 
	    result = CharMatcher.inRange('0', 'z').retainFrom(input);
	    assertEquals("hello", result);
	}
	@Test public void whenRemoveCharsNotInCharset_thenRemoved() {
	    Charset charset = Charset.forName("cp437");
	    final CharsetEncoder encoder = charset.newEncoder();
	 
	    Predicate<Character> inRange = new Predicate<Character>() {
	        @Override
	        public boolean apply(Character c) {
	            return encoder.canEncode(c);
	        }
	    };
	 
	    String result = CharMatcher.forPredicate(inRange)
	                               .retainFrom("helloは");
	    assertEquals("hello", result);
	}
	
	@Test public void whenValidateString_thenValid(){
	    String input = "hello";
	 
	    boolean result = CharMatcher.JAVA_LOWER_CASE.matchesAllOf(input);
	 
	    result = CharMatcher.is('e').matchesAnyOf(input);
	 
	    result = CharMatcher.JAVA_DIGIT.matchesNoneOf(input);
	}
}
